//
//  StatusViewModel.swift
//  CJPWB
//
//  Created by 陈锦彭 on 16/3/17.
//  Copyright © 2016年 chenjinpeng. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    // MARK: - 成员属性
    var status : Status?
    
    init(status : Status) {
        self.status = status
        
        //截取来至于字符串
        if let str = status.source where status.source != ""  {
            //截取出 来自于哪种软件
            let locateIndex = (str as NSString).rangeOfString(">").location + 1
            let length = (str as NSString).rangeOfString("</").location - locateIndex
            sourceStr = "来自 " + (str as NSString).substringWithRange(NSRange(location: locateIndex, length: length))
        }
       
        
        //创建的时间的处理
        if let creatr = status.created_at {
          createAtString = NSDate.getCreatTime(creatr)
        }else {
         createAtString = ""
        }
        
        //认证图标处理
        let verified_type = status.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default :
            verifiedImage = nil
            
        }
        
        //会员的图标处理
        let mbrank = status.user?.mbrank ?? 0
        if mbrank >= 1 && mbrank <= 6 {
            VipImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            VipImage = nil
        }
        
        
        //头像的url处理
        if let url = status.user?.profile_image_url {
         iconUrl = NSURL(string: url)
        }
        
        
        //配图的所有的url处理
        let picurls = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        if let  pic_urls = picurls {
            for picDict in pic_urls {
                let urlStr = picDict["thumbnail_pic"] as? String
                let picUrl = NSURL(string: urlStr ?? "")
                pictureUrls.append(picUrl!)
            }
        }
        
        
    }
    
    
    // MARK: - 自己定义的属性
    /// 来自于
    var sourceStr : String?
    
    ///创建的时间的处理
    var createAtString : String?

    ///认证图标
    var verifiedImage : UIImage?
    ///会员的图标
    var VipImage : UIImage?
    ///头像的url
    var iconUrl : NSURL?
    ///图片所有的url
    var pictureUrls : [NSURL] = [NSURL]()


    
    
    
}
